import { useStore } from 'vuex'
import { LocationQueryRaw, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
export function useRouters(listObj?: any) {
  const router = useRouter()
  const store = useStore()

  function go(params: any) {
    const { name } = params
    const check = store.getters.permission_routes.find((item: any) => {
      if (item.children) {
        const b = item.children.find((element: any) => {
          return element.name === name
        })
        return b !== null
      }
      return item.name === name
    })
    if (check || !name) {
      router.push(params)
    } else {
      ElMessage.error('您暂无访问该页面的权限，请尝试刷新或联系管理员！')
    }
    // store.getters.permission_routes
  }

  function openNewWindow(path: string, query: LocationQueryRaw) {
    const routerData = router.resolve({ path, query })
    window.open(routerData.href, '_blank')
  }
  return {
    go,
    openNewWindow,
  }
}
